/**
 * 处理跳转自定义链接
 * 1.没带http开头的链接，前端自己将当前域名+语言+自定义链接组合成完整的url
 * 2.带了http开头的链接，不需要组合
 * 3.h5的全部在当前页面打开链接
 * 4.PC组合链接当前页面打开，http开头的新标签打开
 */
export function useRedirect() {
  const nuxtApp = useNuxtApp()
  const i18n = nuxtApp.$i18n;
  const { t } = i18n
  const { isLogin } = storeToRefs(useAppStore())
  const { togleCustomerService } = useAppStore()

  // const { isMobile } = storeToRefs(useWindowStore())
  const isMobile = ref(true)


  const notifyThrottle = throttle(() => {
    /* openNotify({
      icon: 'chess-gameshow',
      iconColor: '--tg-text-alltheme-green',
      title: t('activity_tip'),
      message: t('awesome_coming'),
    }) */
  }, 5 * 1000, { leading: true, trailing: false })

  function toCustomUrl(url: string) {
    if (isExternal(url)) {
      if (isMobile.value) {
        location.href = url
      }
      else {
        setTimeout(() => {
          window.open(url, '_blank')
        }, 0)
      }
    }
    else {
      const _url = `${url.startsWith('/') ? url : (`/${url}`)}`
      if (_url.includes('dollar-waves?promoid=')) {
        const redId = _url.split('dollar-waves?promoid=')[1]
        if (redId && +redId > 0) {
          const red_start_stamp = localStorage.getItem(`promo_red_start_${redId}_${getCurrentLanguageForBackend()}`)
          const now_stamp = new Date().getTime()
          if (red_start_stamp && +red_start_stamp > 0) {
            if (now_stamp < +red_start_stamp) {
              notifyThrottle()
              return
            }
          }
        }
      }
      i18nNavigateTo(_url)
    }
  }

  function handleSafe() {
    if (isLogin.value)
      console.log('/')
    // openSafeDialog()
    else
      i18nNavigateTo('/login/signin')
  }

  function jumpToUrl(item: { type?: EnumJump; jumpUrl?: string; jumpState?: string | number; promo_info?: any }) {
    if (item.jumpState && +item.jumpState === 2)
      return
    if (item.promo_info && item.promo_info.id) {
      const now_stamp = new Date().getTime()
      const { display_mode, start_at, end_at } = item.promo_info
      if (display_mode === 2)
        return notifyThrottle()
      if (!start_at || !end_at) {
        return message.error(t('promo_end'))
      }
      if (end_at && +end_at > 0) {
        if (now_stamp > checkTs(+end_at)) {
          return message.error(t('promo_end'))
        }
      }
      if (start_at && +start_at > 0) {
        if (now_stamp < checkTs(+start_at)) {
          notifyThrottle()
          return
        }
      }
    }

    switch (item.type) {
      case EnumJump.CUSTOM:
        if (item.jumpState !== undefined)
          +item.jumpState === 1 && item.jumpUrl && toCustomUrl(item.jumpUrl)
        else
          item.jumpUrl && toCustomUrl(item.jumpUrl)
        break
      case EnumJump.CASINO:
        if (isFlutterApp())
          sendMsgToFlutterApp(SendFlutterAppMessage.CASINO_HOME)
        else
          i18nNavigateTo('/casino')
        break
      case EnumJump.SPORTS:
        if (isFlutterApp())
          sendMsgToFlutterApp(SendFlutterAppMessage.SPORTS_HOME)
        else
          i18nNavigateTo(`/sports/${getSportsPlatId()}`)
        break
      case EnumJump.SAFE:
        if (isFlutterApp())
          sendMsgToFlutterApp(SendFlutterAppMessage.SAFE_VAULT)
        else
          handleSafe()
        break
      case EnumJump.VIP:
        if (isFlutterApp())
          sendMsgToFlutterApp(SendFlutterAppMessage.USER_VIP)
        else
          i18nNavigateTo('/vip/vip-bonus')
        break
      case EnumJump.ALLIANCE:
        if (isFlutterApp())
          sendMsgToFlutterApp(SendFlutterAppMessage.USER_AFFILIATE)
        else
          i18nNavigateTo('/affiliate/affiliate-tutorial')
        break
      case EnumJump.PROMOTION:
        if (isFlutterApp())
          sendMsgToFlutterApp(SendFlutterAppMessage.ALL_PROMOTION)
        else
          i18nNavigateTo('/promotion')
        break
      case EnumJump.SERVICE:
        if (isFlutterApp())
          sendMsgToFlutterApp(SendFlutterAppMessage.OPEN_SERVICE)
        else
          togleCustomerService()
        break
      case EnumJump.DEPOSIT:
        if (isFlutterApp())
          sendMsgToFlutterApp(SendFlutterAppMessage.OPEN_DEPOSIT_MODAL)
        else
          i18nNavigateTo('/deposit')
        break
      case EnumJump.REBATE:
        if (isFlutterApp())
          sendMsgToFlutterApp(SendFlutterAppMessage.USER_REBATE)
        else
          i18nNavigateTo('/vip/rebate')
        break
      case EnumJump.REGISTER:
        if (isLogin.value) {
          if (isFlutterApp())
            sendMsgToFlutterApp(SendFlutterAppMessage.CASINO_HOME)
          else
            i18nNavigateTo('/casino')
          return
        }
        if (isFlutterApp())
          sendMsgToFlutterApp(SendFlutterAppMessage.DIALOG_REGISTER)
        else
          i18nNavigateTo('/login/regist')
        break
      case EnumJump.APP:
        // openAppDownloadDialog()
        break
      case EnumJump.SETTINGS:
        if (isLogin.value) {
          if (isFlutterApp())
            sendMsgToFlutterApp(SendFlutterAppMessage.USER_SETTINGS)
          else
            i18nNavigateTo('/settings/general')
        }
        else {
          if (isFlutterApp())
            sendMsgToFlutterApp(SendFlutterAppMessage.DIALOG_REGISTER)
          else
            i18nNavigateTo('/login/regist')
        }
        break
      case EnumJump.PROMOTION_DETAIL:
        item.jumpUrl && toCustomUrl(item.jumpUrl)
        break
    }
  }

  return {
    toCustomUrl,
    jumpToUrl,
  }
}
